<template>
  <div>
    <div style="display: flex;justify-content: space-between;margin: 5px 0">
      <el-input placeholder="请输入异常名查询" v-model.trim="search" style="margin-bottom: 5px;width: 300px">
        <el-button type="primary" slot="append" icon="el-icon-search" @click="handleQuery"></el-button>
      </el-input>
    </div>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="exceptionName" label="异常类别" min-width="100" align="center"></el-table-column>
      <el-table-column prop="message" label="信息" min-width="100" align="center"></el-table-column>
      <el-table-column prop="url" label="请求路径" min-width="100" align="center"></el-table-column>
      <el-table-column prop="params" label="参数" min-width="100" align="center"></el-table-column>
      <el-table-column prop="username" label="请求人" min-width="50" align="center"></el-table-column>
      <el-table-column prop="time" label="时间" min-width="70" align="center"></el-table-column>
      <el-table-column align="center" fixed="right" label="操作" min-width="50">
        <template slot-scope="scope">
          <el-button-group>
            <el-button @click="handleDelete(scope.row)" type="danger" size="mini">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-row>
      <el-col :offset="14" :span="9">
        <el-pagination
          @current-change="currentChange"
          :page-size="pageSize"
          layout="prev, pager, next"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import dayjs from 'dayjs'

export default {
  name: "LogManager",
  data() {
    return {
      tableData: [],
      search: '',
      pageSize: 9,  //每页显示条数
      total: 0,    //总条数
      currentPage: 1,  //当前页
    }
  },
  methods: {
    handleQuery() {
      this.currentPage = 1;
      this.getData();
    },
    handleDelete(data) {
      this.$confirm('确认删除?', '删除', {type: 'warning'}).then(() => {
        this.$axios.post('/log/deleteById', this.$qs.stringify({id: data.id})).then(res => {
          if (res.code === 200) {
            this.getData();
            this.$message({type: 'success', message: '删除成功!'});
          }
        })
      }).catch(() => {
      });
    },
    currentChange(data) {
      this.currentPage = data;
      this.getData();
    },
    getData() {
      this.$axios.post('/log/findByParams', this.$qs.stringify({
        name: this.search,
        page: this.currentPage,
        size: this.pageSize
      })).then(res => {
        if (res.code === 200) {
          for (let item of res.data.data.content) {
            item.time = dayjs(item.time).format('YYYY-MM-DD : HH');
          }
          this.tableData = res.data.data.content;
          this.total = res.data.data.totalElements;
        }
      })
    }
  }, created() {
    this.getData();
  }
}
</script>
